package com.akk.platform.manage;

import com.akk.module.avs.entity.SynthesisFinancial;
import com.akk.module.avs.model.request.SynthesisFinancialRequest;
import com.akk.module.avs.service.SynthesisFinancialService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "综合金融管理", description = "综合金融管理接口")
@RequestMapping(value = "/platform/manage/synthesisFinancial")
public class SynthesisFinancialAction {

    @Autowired
    private SynthesisFinancialService synthesisFinancialService;
    @Autowired
    private PictureService pictureService;

    @RequestMapping(value = "/{appId}/list", method = RequestMethod.GET)
    @ApiOperation(value = "分页查询")
    @RequiresPermissions("sys:synthesisFinancial:list")
    public RestResponse pageList(@PathVariable String appId, @Valid Query query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<SynthesisFinancial> list = synthesisFinancialService.findByAppId(appId);
        return RestResponse.build(PageUtils.buildPage(list, (int) new PageInfo<>(list).getTotal(), query));
    }

    @RequestMapping(value = "/{appId}/insert", method = RequestMethod.POST)
    @ApiOperation(value = "添加数据")
    @RequiresPermissions("sys:synthesisFinancial:insert")
    public RestResponse insert(SynthesisFinancialRequest synthesisFinancial, @PathVariable String appId) {
        if (synthesisFinancial.getFile() == null)
            throw new BusinessException("请上传文件");
        Map result = pictureService.uploadPicture(synthesisFinancial.getFile());
        if (Integer.valueOf(result.get("status").toString()) == 0) {
            synthesisFinancial.setImage(result.get("url").toString());
            synthesisFinancial.setAppId(appId);
            synthesisFinancialService.insert(synthesisFinancial);
            return RestResponse.build("success");
        }
        throw new BusinessException("文件上传失败");
    }

    @RequestMapping(value = "/{appId}/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改数据")
    @RequiresPermissions("sys:synthesisFinancial:update")
    public RestResponse update(SynthesisFinancialRequest synthesisFinancial, @PathVariable String appId) {
        if (!synthesisFinancial.getAppId().equals(appId))
            throw new BusinessException("非法操作");
        SynthesisFinancial financial = synthesisFinancialService.findById(synthesisFinancial.getId());
        if (StringUtils.hasText(synthesisFinancial.getAppId()))
            financial.setAppId(synthesisFinancial.getAppId());
        if (StringUtils.hasText(synthesisFinancial.getSfStatus()))
            financial.setSfStatus(synthesisFinancial.getSfStatus());
        if (StringUtils.hasText(synthesisFinancial.getShowName()))
            financial.setShowName(synthesisFinancial.getShowName());
        if (StringUtils.hasText(synthesisFinancial.getType()))
            financial.setType(synthesisFinancial.getType());
        if (StringUtils.hasText(synthesisFinancial.getUrl()))
            financial.setUrl(synthesisFinancial.getUrl());
        if (StringUtils.hasText(synthesisFinancial.getTag()))
            financial.setTag(synthesisFinancial.getTag());
        financial.setSortIndex(synthesisFinancial.getSortIndex());
        if (synthesisFinancial.getFile() != null) {
            Map result = pictureService.uploadPicture(synthesisFinancial.getFile());
            if (Integer.valueOf(result.get("status").toString()) == 0) {
                pictureService.removeFile(financial.getImage());
                financial.setImage(result.get("url").toString());
            } else {
                throw new BusinessException("文件上传失败");
            }
        }
        synthesisFinancialService.update(financial);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "详情")
    @RequiresPermissions("sys:synthesisFinancial:info")
    public RestResponse info(@PathVariable Integer id) {
        return RestResponse.build(synthesisFinancialService.findById(id));
    }
}
